package com.example.hemin.fnb.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.adapter.ShoppingCarAdapter;
import com.example.hemin.fnb.ui.base.BaseActivity;
import com.example.hemin.fnb.ui.bean.ShoppingCarBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShoppingCar extends BaseActivity {
    @BindView(R.id.lay_back)
    LinearLayout layBack;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.image)
    CheckBox image;
    @BindView(R.id.image2)
    TextView image2;
    @BindView(R.id.shopping_car_recyclerview)
    RecyclerView shoppingCarRecyclerview;
    @BindView(R.id.b_titl1)
    TextView bTitl1;
    @BindView(R.id.b_titl2)
    TextView bTitl2;
    @BindView(R.id.b_titl3)
    TextView bTitl3;
    @BindView(R.id.lay1)
    LinearLayout lay1;
    @BindView(R.id.lay2)
    LinearLayout lay2;
    @BindView(R.id.title1)
    TextView title1;
    @BindView(R.id.delect)
    TextView delect;
    private List<ShoppingCarBean> beans = new ArrayList<>();
    private boolean status = false;
    private int numbers = 0;
    private int total = 0;
    private int totals = 0;
    private boolean CheckBoxStatus = false;
    private ShoppingCarAdapter adapter = new ShoppingCarAdapter(R.layout.shopapping_adapter_item, beans);


    @Override
    public int getLayoutId() {
        return R.layout.activity_shopping_car;
    }

    @Override
    public void initView() {
        title.setText("购物车");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initDate();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        shoppingCarRecyclerview.setLayoutManager(linearLayoutManager);
        adapter = new ShoppingCarAdapter(R.layout.shopapping_adapter_item, beans);
        shoppingCarRecyclerview.setAdapter(adapter);
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(AppUtils.getContext(),ShoppingCarItemActivity.class);
//                startActivity(intent);
//            }
//        });


    }

    private void initDate() {
        for (int i = 0; i < 5; i++) {
            ShoppingCarBean bean = new ShoppingCarBean("wdwadawd", "作曲家", 3000, 10);
            ShoppingCarBean bea2 = new ShoppingCarBean("wdwadwdaw", "绘画家", 6666, 0);
            beans.add(bean);
            beans.add(bea2);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.lay_back, R.id.image, R.id.image2, R.id.b_titl3, R.id.title1,R.id.delect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_back:
                finish();
                break;
            case R.id.image:
                if (CheckBoxStatus == false) {
                    image.setChecked(true);
                    adapter.selectAll();
                    CheckBoxStatus = true;
                } else if (CheckBoxStatus == true) {
                    image.setChecked(false);
                    adapter.unSelectAll();
                    CheckBoxStatus = false;
                }

                break;
            case R.id.image2:
                if (status == false) {
                    lay1.setVisibility(View.GONE);
                    lay2.setVisibility(View.VISIBLE);
                    status = true;
                } else if (status == true) {
                    lay1.setVisibility(View.VISIBLE);
                    lay2.setVisibility(View.GONE);
                    status = false;
                }
                break;
            case R.id.b_titl3:
                break;
            case R.id.delect:
                adapter.deletingData();
                break;

        }
    }

    @Subscribe(id = 21)
    public void getNumber(Map map) {
        int number = (int) map.get("number");
        int pice = (int) map.get("pice");
        total = number * pice;
        bTitl1.setText(String.valueOf(total));
        bTitl2.setText(String.valueOf(number));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
